/*
mb-gadget - a google gadget for Moneybookers.com
Copyright (c) 2008, Ivan Mikushin
All rights reserved.

Distributed under GNU General Public License, see http://www.gnu.org/licenses/
*/

Ext.BLANK_IMAGE_URL = 'lib/ext-2/resources/images/default/s.gif';

var MbGadget = function() {
    var BALANCE_URL = 'https://www.moneybookers.com/app/?rid=3594792';
    var LOGOUT_URL = 'https://www.moneybookers.com/app/logout.pl';
    
    var balanceRefresher = new Ext.util.DelayedTask();
    
    var backgroundFrame = null;
    
    return {
        init: function() {
            var fg = Ext.get('balance_frame_0');
            var bg = Ext.get('balance_frame_1');
            this.visualizeFrames(fg, bg);
            this.refreshBalance();
        },
        
        refreshBalance: function() {
            Ext.fly('balanceFrameContainer').mask('account info...' , 'x-mask-loading');
            backgroundFrame.dom.src = BALANCE_URL;
        },
        
        visualizeFrames: function(fg, bg) {
            fg.setVisible(true);
            bg.setVisible(false);
            bg.on('load', function(){this.swapFrames(bg, fg)}, this, {single: true, stopEvent: true});
            backgroundFrame = bg;
        },
        
        swapFrames: function(fg, bg) {
            this.visualizeFrames(fg, bg);
            Ext.fly('balanceFrameContainer').unmask();
            balanceRefresher.delay(120000, this.refreshBalance, this);
        },
        
        logOut: function() {
            balanceRefresher.cancel();
            backgroundFrame.on('load', function(){this.refreshBalance()}, this, {single: true, stopEvent: true});
            backgroundFrame.dom.src = LOGOUT_URL;
        }
    };
}();

Ext.onReady(MbGadget.init, MbGadget, true);
